package com.ruoyi.dzzh.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.dzzh.domain.FileReport;
import com.ruoyi.dzzh.mapper.DzzhReportMapper;
import com.ruoyi.dzzh.domain.DzzhReport;
import com.ruoyi.dzzh.service.IDzzhReportService;

/**
 * 灾情速报Service业务层处理
 * 
 * @author cjt
 * @date 2023-03-15
 */
@Service
public class DzzhReportServiceImpl implements IDzzhReportService 
{
    @Autowired
    private DzzhReportMapper dzzhReportMapper;

    /**
     * 查询灾情速报
     * 
     * @param reportId 灾情速报主键
     * @return 灾情速报
     */
    @Override
    public DzzhReport selectDzzhReportByReportId(Long reportId)
    {
        return dzzhReportMapper.selectDzzhReportByReportId(reportId);
    }

    /**
     * 查询灾情速报列表
     * 
     * @param dzzhReport 灾情速报
     * @return 灾情速报
     */
    @Override
    public List<DzzhReport> selectDzzhReportList(DzzhReport dzzhReport)
    {
        return dzzhReportMapper.selectDzzhReportList(dzzhReport);
    }

    /**
     * 新增灾情速报
     * 
     * @param dzzhReport 灾情速报
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDzzhReport(DzzhReport dzzhReport)
    {
        dzzhReport.setCreateTime(DateUtils.getNowDate());
        int rows = dzzhReportMapper.insertDzzhReport(dzzhReport);
        insertFileReport(dzzhReport);
        return rows;
    }

    /**
     * 修改灾情速报
     * 
     * @param dzzhReport 灾情速报
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDzzhReport(DzzhReport dzzhReport)
    {
        dzzhReport.setUpdateTime(DateUtils.getNowDate());
        dzzhReportMapper.deleteFileReportByReportId(dzzhReport.getReportId());
        insertFileReport(dzzhReport);
        return dzzhReportMapper.updateDzzhReport(dzzhReport);
    }

    /**
     * 批量删除灾情速报
     * 
     * @param reportIds 需要删除的灾情速报主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDzzhReportByReportIds(Long[] reportIds)
    {
        dzzhReportMapper.deleteFileReportByReportIds(reportIds);
        return dzzhReportMapper.deleteDzzhReportByReportIds(reportIds);
    }

    /**
     * 删除灾情速报信息
     * 
     * @param reportId 灾情速报主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDzzhReportByReportId(Long reportId)
    {
        dzzhReportMapper.deleteFileReportByReportId(reportId);
        return dzzhReportMapper.deleteDzzhReportByReportId(reportId);
    }

    /**
     * 新增速报附件信息
     * 
     * @param dzzhReport 灾情速报对象
     */
    public void insertFileReport(DzzhReport dzzhReport)
    {
        List<FileReport> fileReportList = dzzhReport.getFileReportList();
        Long reportId = dzzhReport.getReportId();
        if (StringUtils.isNotNull(fileReportList))
        {
            List<FileReport> list = new ArrayList<FileReport>();
            for (FileReport fileReport : fileReportList)
            {
                fileReport.setReportId(reportId);
                list.add(fileReport);
            }
            if (list.size() > 0)
            {
                dzzhReportMapper.batchFileReport(list);
            }
        }
    }
}
